<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Model\Hint;
use App\Http\Services\EquipService;
use App\Http\Services\UserServices;
use App\Lib\Response;

class EquipAuctionController extends BaseController
{
    protected $equipService;//装备服务
    protected $userService;//基础服务

    public function __construct()
    {
        $this->equipService = new EquipService();
        $this->userService = new UserServices();//公共api
    }

    //获取用户所持装备
    public function getUserEquip(Request $request){
        $user_id = (int) $request->get('user_id', 0);
        $page = (int) $request->get('page', 1);
        $limit = (int) $request->get('limit', 10);
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR,[], '未登录');
        }
        //装备分类
        $equip_type = (int) $request->get('equip_type', 0);

        $res = $this->equipService->getUserEquip($user_id,$equip_type,$page,$limit);
        Response::returnResult(Hint::SUCCESS,$res);
    }
    
    //拍卖上架
    public function sellEquip(Request $request)
    {
        //接收参数
        $equipId = (int) $request->post('equipid',0);
        if(empty($equipId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //用户id
        $user_id = (int) $request->post('user_id',0);
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //用户是否存在
        $userinfo = $this->userService->getUserInfoByUuid($user_id);
        if(empty($userinfo)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //售卖数量
        $sellNums = (int) $request->post('nums',0);

        if($sellNums < 1){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //售卖单价
        $price = $request->post('price',0);
        if($price < 1){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //房间id
        $roomId = (int) $request->post('roomid',0);

        //入库处理
        $status = $this->equipService->insertSell($equipId, $user_id, $sellNums, $price, $roomId);
        
        if($status['code'] != 0){
            Response::returnResult(Hint::ERR_NORMAL, [], $status['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $status['data'], $status['msg']);
    }

    //拍卖下架
    public function unsaleEquip(Request $request){
        //用户id
        $user_id = (int) $request->post('user_id',0);
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '未登录');
        }
        $auctionId = (int) $request->post('auction_id',0);
        if(empty($auctionId)){
            Response::returnResult(Hint::PARAM_ERROR, [], '缺少拍卖id');
        }
        $status = $this->equipService->unsaleEquip($user_id,$auctionId);
        if($status['code'] != 1){
            Response::returnResult(Hint::ERR_NORMAL, [], $status['msg']);
        }
        Response::returnResult(Hint::SUCCESS, [], '成功');
    }
    
    //拍卖列表
    public function shoplist(Request $request)
    {
        //当前页码
        $page = (int) $request->get('page',1);
        //每页显示条数
        $limit = (int) $request->get('limit',10);
        //用户id
        $user_id = (int) $request->get('user_id',0);
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '未登录');
        }

        //关键词
        $kwd = $request->get('kwd',0);
        //装备分类
        $equip_type = $request->get('equip_type',0);
        //装备稀有度
        $equip_level = (int) $request->get('equip_level',0);

        //获取数据
        $data = $this->equipService->getShopList($page, $limit, $kwd, $equip_type, $equip_level,$user_id);

        //获取用户当前所持金豆数
        $userJdCount = $this->equipService->getUserJd($user_id);
        $data['userJdCount'] = $userJdCount;
        Response::returnResult(Hint::SUCCESS, $data, '成功');
    }

    //获取装备分类
    public function getEquipType()
    {
        $data = $this->equipService->getEquipType();
        Response::returnResult(Hint::SUCCESS, $data, '成功');
    }

    //用户购买拍卖行装备条件判断
    public function buyEquip(Request $request){
        $user_id = $request->post('user_id',0);//用户id
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '未登录');
        }

        $buy_id = (int) $request->post('auction_id',0);//购买拍卖行id
        if(empty($buy_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '缺少拍卖id');
        }
        //购买逻辑
        $res = $this->equipService->buyEquip($user_id,$buy_id);
        if($res['code'] != 1){
            Response::returnResult(Hint::ERR_NORMAL, [], $res['msg']);
        }
        Response::returnResult(Hint::SUCCESS, [], $res['msg']);
    }

    //获取拍卖记录
    public function auctionRecord(Request $request){
        $user_id = (int) $request->get('user_id',0);//用户id
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '未登录');
        }
        $type = (int) $request->get('type',0);//类型1出售记录2购买记录
        if(empty($type)){
            Response::returnResult(Hint::PARAM_ERROR, [], '拍卖类型不能为空');
        }
        $room_id = (int) $request->get('room_id',0);//房间id
        $page = (int) $request->get('page',1);//当前页
        $limit = (int) $request->get('limit',10);//每页条数
        //获取数据
        $res = $this->equipService->auctionRecord($user_id, $type, $page, $limit, $room_id);
        //获取用户当前所持金豆数
        $userJdCount = $this->equipService->getUserJd($user_id);
        $res['userJdCount'] = $userJdCount;
        Response::returnResult(Hint::SUCCESS, $res, '成功');
    }

    //获取查看自己房间拍卖记录权限
    public function recordSpecial(Request $request){
        $user_id = $request->get("user_id",0);//登录用户id
        $room_id = $request->get("room_id",0);//当前房间id
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '未登录');
        }
        //验证权限
        $role = $this->equipService->getMenu($user_id,$room_id);
        Response::returnResult(Hint::SUCCESS, $role, '成功');
    }

    //获取费率
    public function rate(){
        $data = $this->equipService->getRate();
        Response::returnResult(Hint::SUCCESS, $data, '成功');
    }

    //获取用户是否被禁言
    public function isForbidden(Request $request){
        $user_id = $request->get('user_id',0);
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR, [], '未登录');
        }
        $status = $this->equipService->isForbidden($user_id);
        Response::returnResult(Hint::SUCCESS, $status);
    }

    //举报公聊消息
    public function reportAuctionMsg(Request $request){
        $user_id = $request->post("user_id"); //发起举报用户id
        $report_userId = $request->post("report_user_id");//被举报人id
        $report_type = $request->post("report_type"); //举报类型
        $pics = $request->post("pics"); //图片
        $describe = $request->post("describe"); //举报描述
        $sendMsg = $request->post("msg");//举报文案

        if (empty($user_id) || empty($report_userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (empty($report_type)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if ($pics == null){
            $pics = "";
        }
        if ($describe == null){
            $describe = "";

        }
        $ret = $this->equipService->add_report($user_id, $report_type, $pics, $describe, $sendMsg, $report_userId);
        if ($ret['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,[],$ret['msg']);
        }
        Response::returnResult(Hint::SUCCESS);
    }
}